using UnityEngine;
using UnityEngine.UI;


public enum FireState {
    none,
    start,
    puting,
    end
}

public class FirewallSpawner : MonoBehaviour
{

    public GameObject firewallPrefab; // 防火墙对象的预制体
    public Button placementButton; // 放置按钮的引用
    public float spawnDuration = 10f; // 防火墙的持续时间
    public FireState state =FireState.none;
    private bool isPlacingFirewall = false; // 控制是否处于放置防火墙模式

    // 调用此方法以切换放置防火墙模式
    public void ToggleFirewallPlacement()
    {
        state = FireState.start;
           //Invoke("ReleaseAction", 0.1f);
           isPlacingFirewall = true;
        //placementButton.interactable = !isPlacingFirewall; // 切换按钮的可交互状态
    }

    void Update()
    {
        if (state == FireState.start && Input.GetMouseButtonDown(0)) {
            state = FireState.puting;
        }

        if (state == FireState.puting && Input.GetMouseButton(0)) // 按住鼠标左键
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                // 假设地面有一个特定的标签，如"Ground"
                if (hit.collider.CompareTag("Ground") || hit.collider.CompareTag("Road"))
                {
                    Vector3 spawnPosition = hit.point;
                    GameObject firewall = Instantiate(firewallPrefab, spawnPosition, Quaternion.identity);
                    firewall.AddComponent<FirewallLifetime>().Init(spawnDuration);
                }
            }
            
        }
        if (state == FireState.puting && Input.GetMouseButtonUp(0)) {
            state = FireState.none;
        }
        
        //if (Input.GetMouseButtonUp(0) && isPlacingFirewall)
        //{
        //    // 鼠标左键被松开时执行的操作
        //    Debug.Log("鼠标左键被松开");
        //    isPlacingFirewall = false;
        //}
        
        
    }
}